package pl.put.dcs.workManagerServer.projectManager.queueManager.taskWatchdog;

import java.util.Calendar;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import org.apache.log4j.Logger;
import org.jgroups.util.Scheduler;
import pl.put.dcs.workManagerServer.config.ServerConfigManager;
import pl.put.dcs.workManagerServer.projectManager.queueManager.cacheManeger.CacheManagerImpl.CacheManagerImpl;
import pl.put.dcs.workManagerServer.projectManager.queueManager.taskWatchdog.listeners.PacketStatusListener;
import pl.put.dcs.workManagerServer.projectManager.queueManager.taskWatchdog.listeners.PacketStatusListener.STATUS;
import pl.put.dcs.workManagerServer.serviceConnectors.ControlModuleServerConnector;
import pl.put.model.packet.Packet;

// <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
// #[regen=yes,id=DCE.20E421E7-9304-2343-FDB4-46C74AD71A04]
// </editor-fold> 
public class TaskWatchdog extends TimerTask
{
    public static Logger log = Logger.getLogger( TaskWatchdog.class.getName() );

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.CD9FDCFB-EAAA-C68C-A0DE-03103AD6C426]
    // </editor-fold> 
    private PacketStatusListener packetStatusListener;

    private Timer timer = new Timer();
    private int maxPackets;
    ControlModuleServerConnector controlModuleServerConnector;

    private CacheManagerImpl cache;

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.AD17DCB2-AE69-10DD-622F-67241705170C]
    // </editor-fold> 
    public TaskWatchdog( CacheManagerImpl cache )
    {
        this.cache = cache;
        maxPackets = ServerConfigManager.getMaxTaskForTaskWachdog();
        controlModuleServerConnector = ( ControlModuleServerConnector )ControlModuleServerConnector.getInstance();
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.3259246A-9044-F866-5ABC-207D6D3994CA]
    // </editor-fold> 
    public boolean isPacketTerminated( Packet packet )
    {
        packet = cache.getPacketFromCache( packet.getId().toString() );
        Calendar terminateDate  = Calendar.getInstance();
        terminateDate.setTime( packet.getTerminationDate() );
        Calendar now = Calendar.getInstance();
        if ( now.compareTo( terminateDate ) > 0 )
        {
            return true;
        }
        return false;
    }

    public void findTerminatedPackets()
    {
        log.debug( "Start search terminated packets" );
        List<Packet> terminatedPackets = controlModuleServerConnector.getTerminatedTasksIdsLimited( maxPackets );
        if ( terminatedPackets != null )
        {
            if ( terminatedPackets.size() > 0 )
            {
                log.debug( "Found terminated packets. Ids:" );
            }
            for ( Packet packet : terminatedPackets )
            {
                log.debug( packet.getId().toString() );
                packet = cache.popPacketFromCache( packet.getId().toString() );
                packetStatusListener.packetStatusChanged( packet, STATUS.TERMINATED );
            }
        }
        log.debug( "END findTerminatedPackets");
    }

    public void start()
    {
        timer.schedule( this, 0, 10000 );

    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.0B0F174A-28DC-CE73-9607-381BEC3864CD]
    // </editor-fold> 
    public void run()
    {
        findTerminatedPackets();
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,regenBody=yes,id=DCE.075A6FA0-B093-6FB4-6E07-D6555EB2B093]
    // </editor-fold> 
    public PacketStatusListener getPacketStatusListener()
    {
        return packetStatusListener;
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,regenBody=yes,id=DCE.49FA5801-8F05-7F8E-08FA-FB03FD50187C]
    // </editor-fold> 
    public void setPacketStatusListener( PacketStatusListener val )
    {
        this.packetStatusListener = val;
    }
}

